//
//  InterfaceTool.m
//  接口工具类
//
//  Created by 吴俊杰 on 2016/11/12.
//  Copyright © 2016年 吴俊杰. All rights reserved.
//

#import "InterfaceTool.h"

@implementation InterfaceTool

/**
 获取单例对象
 */
+ (instancetype)sharedInterfaceTool{
    
    static InterfaceTool *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[InterfaceTool alloc] init];
    });
    return instance;
}

/**
 天气预报
 */
- (void)getWeatherSuccess:(void(^)(id response))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    NSString *key = @"17IvzuqmKrK1cGwNL6VQebF9";
    NSString *url = @"http://api.map.baidu.com/telematics/v3/weather";
    NSDictionary *parameters = @{@"location":@"广州",@"output":@"json",@"ak":key};
    
    [self getURL:url parameter:parameters success:^(id response) {
        successBlock(response);
    } failure:^(NSError *error) {
        failureBlock(error);
    }];
}

/**
 获取登陆
 */
- (void)getLoginSuccess:(void(^)(id response))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    NSString *url = @"http://iosapi.itcast.cn/doctor/carelinkQuickLogin.json.php";
    NSDictionary *parameters = @{@"login_token":@"985c96ce43bb5ef69de3e1ad8afaa83f"};
    
    [self postURL:url parameter:parameters success:^(id response) {
        successBlock(response);
    } failure:^(NSError *error) {
        failureBlock(error);
    }];
}

/**
 获取轮播图片
 */
- (void)getScrollImageSuccess:(void(^)(id response))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    NSString *url = @"http://iosapi.itcast.cn/doctor/banners.json.php";
    NSDictionary *parameters = @{@"page_size":@10,@"page":@1};

    [self postURL:url parameter:parameters success:^(id response) {
        successBlock(response);
    } failure:^(NSError *error) {
        failureBlock(error);
    }];
}

/**.
 选择疾病细分列表
 */
- (void)getSelectDiseaseSuccess:(void(^)(id response))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    NSString *url = @"http://iosapi.itcast.cn/doctor/searchCI3List.json.php";
    NSDictionary *parameters = @{@"page":@1,@"page_size":@15,@"ci1_id":@1,@"keyword":@""};
    
    [self postURL:url parameter:parameters success:^(id response) {
        successBlock(response);
    } failure:^(NSError *error) {
        failureBlock(error);
    }];
}

/**
 选择并发症列表
 */
- (void)getSelectComplicationSuccess:(void(^)(id response))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    NSString *url = @"http://iosapi.itcast.cn/doctor/complicationList.json.php";
    NSDictionary *parameters = @{@"page":@1,@"page_size":@15,@"ci1_id":@1,@"ci2_id":@3};
    
    [self postURL:url parameter:parameters success:^(id response) {
        successBlock(response);
    } failure:^(NSError *error) {
        failureBlock(error);
    }];
}

/**
 获取医生数量
 */
- (void)getDoctorCountSuccess:(void(^)(id response))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    NSString *url = @"http://iosapi.itcast.cn/doctor/matchedDoctorCount.json.php";
    NSDictionary *parameters = @{@"ci1_id":@1,@"ci2_id":@3,@"ci3_id":@3,@"diagnosis_type":@0,@"is_confirmed":@1,@"user_id":@1000089, @"has_diagnosis":@2};
    
    [self postURL:url parameter:parameters success:^(id response) {
        successBlock(response);
    } failure:^(NSError *error) {
        failureBlock(error);
    }];
}

/**
 获取医生列表
 */
- (void)getDoctorListSuccess:(void(^)(id response))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    NSString *url = @"http://iosapi.itcast.cn/doctor/matchDoctors.json.php";
    NSDictionary *parameters = @{@"ci1_id":@1,@"ci2_id":@3,@"ci3_id":@3,@"diagnosis_type":@0,@"page_size":@15,@"is_confirmed":@1,@"user_id":@1000089, @"page":@1,@"has_diagnosis":@2};
    
    [self postURL:url parameter:parameters success:^(id response) {
        successBlock(response);
    } failure:^(NSError *error) {
        failureBlock(error);
    }];
}

/**
 获取医生基本信息
 */
- (void)getDoctorInfoSuccess:(void(^)(id response))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    NSString *url = @"http://iosapi.itcast.cn/doctor/getDoctorInfo.json.php";
    NSDictionary *parameters = @{@"user_id":@1000089,@"doctor_id":@(300000315)};
    
    [self postURL:url parameter:parameters success:^(id response) {
        successBlock(response);
    } failure:^(NSError *error) {
        failureBlock(error);
    }];
}

/**
 获取医生的接诊条件
*/
- (void)getDoctorConditionSuccess:(void(^)(id response))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    NSString *url = @"http://iosapi.itcast.cn/doctor/doctorReceivingSetting.json.php";
    NSDictionary *parameters = @{@"doctor_id":@(300000315)};
    
    [self postURL:url parameter:parameters success:^(id response) {
        successBlock(response);
    } failure:^(NSError *error) {
        failureBlock(error);
    }];
}

/**
 获取医生简介
 */
- (void)getDoctorProfileSuccess:(void(^)(id response))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    NSString *url = @"http://iosapi.itcast.cn/doctor/getIntroduction.json.php";
    NSDictionary *parameters = @{@"doctor_id":@(300000315)};
    
    [self postURL:url parameter:parameters success:^(id response) {
        successBlock(response);
    } failure:^(NSError *error) {
        failureBlock(error);
    }];
}

/**
 获取医生就诊时间
 */
- (void)getDoctorWorkTimeSuccess:(void(^)(id response))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    NSString *url = @"http://iosapi.itcast.cn/doctor/doctorDuties.json.php";
    NSDictionary *parameters = @{@"doctor_id":@(300000315)};
    
    [self postURL:url parameter:parameters success:^(id response) {
        successBlock(response);
    } failure:^(NSError *error) {
        failureBlock(error);
    }];
}

/**
 获取关注医生
 */
- (void)getFollowDoctorSuccess:(void(^)(id response))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    NSString *url = @"http://iosapi.itcast.cn/doctor/doctorDuties.json.php";
    NSDictionary *parameters = @{@"doctor_id":@(300000315)};
    
    [self postURL:url parameter:parameters success:^(id response) {
        successBlock(response);
    } failure:^(NSError *error) {
        failureBlock(error);
    }];
}

/**
 获取取消关注医生
 */
- (void)getRemovedDoctorSuccess:(void(^)(id response))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    NSString *url = @"http://iosapi.itcast.cn/doctor/doctorDuties.json.php";
    NSDictionary *parameters = @{@"doctor_id":@(300000315)};
    
    [self postURL:url parameter:parameters success:^(id response) {
        successBlock(response);
    } failure:^(NSError *error) {
        failureBlock(error);
    }];
}

/**
 获取关注医生的列表
 */
- (void)getFollowedDoctorListWithPage:(NSInteger)page success:(void(^)(id response))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    NSString *url = @"http://iosapi.itcast.cn/doctorList.json.php";
    NSDictionary *parameters = @{@"user_id":@1000089,@"page_size":@15,@"page":@(page)};
    
    [self postURL:url parameter:parameters success:^(id response) {
        successBlock(response);
    } failure:^(NSError *error) {
        failureBlock(error);
    }];
}

/**
 根据疾病和页数获取疾病细分

 @param disease 疾病的枚举
 @param page 加载的页数
 */
- (void)getSubsectionWithDisease:(Disease)disease Page:(int)page success:(void(^)(id response))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    NSString *url = @"http://iosapi.itcast.cn/doctor/searchCI3List.json.php";
    NSDictionary *parameters = @{@"page":@(page),@"page_size":@15,@"ci1_id":@(disease),@"keyword":@""};

    [self postURL:url parameter:parameters success:^(id response) {
        successBlock(response);
    } failure:^(NSError *error) {
        failureBlock(error);
    }];
}

#pragma mark - 获取URL主方法
/**
 get请求获取URL
 
 @param URL URL字符串
 @param parameters 请求参数
 */
- (void)getURL:(NSString *)URL parameter:(NSDictionary *)parameters success:(void(^)(id response))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    [[NetworkTool sharedNetworkTool] GETWithURLString:URL parameters:parameters success:^(id response) {
        successBlock(response);
    } failure:^(NSError *error) {
        failureBlock(error);
    }];
}

/**
 post请求获取URL

 @param URL URL字符串
 @param parameters 请求的参数
 */
- (void)postURL:(NSString *)URL parameter:(NSDictionary *)parameters success:(void(^)(id response))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    NetworkTool *networkTool = [NetworkTool sharedNetworkTool];
    //需要json请求
    networkTool.requestSerializer = [AFJSONRequestSerializer serializer];
    //需要设置返回值类型修改
    networkTool.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",nil];
    [networkTool POSTWithURLString:URL parameters:parameters success:^(id response) {
        successBlock(response);
    } failure:^(NSError *error) {
        failureBlock(error);
    }];
}

@end
